<template>
  <view class="container">
    <view class="example">
     <uni-forms ref="form" :model="formData" labelWidth="18%">
		  <uni-forms-item label="周数" name="weeks" required>
			<span style="line-height: 35px">第{{ formData.weeks}}周</span>
		  </uni-forms-item>
		  <uni-forms-item label="日期" name="date" required>
			<uni-datetime-picker 
			type="date" 
			:clear-icon="false" 
			v-model="formData.date" 
			@change="dateChange"/>
		  </uni-forms-item>
		  <uni-forms-item label="鲤城区" name="lc" v-if="0 == formData.current  || formData.add">
			<dragTextarea :list="formData.lcList" type="lc"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.lc" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="丰泽区" name="fz" v-if="1 == formData.current || formData.add">
		    <dragTextarea :list="formData.fzList" type="fz"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.fz" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="洛江区" name="lj" v-if="2 == formData.current || formData.add">
			<dragTextarea :list="formData.ljList" type="lj"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.lj" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="泉港区" name="qg" v-if="3 == formData.current || formData.add">
			<dragTextarea :list="formData.qgList" type="qg"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.qg" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="石狮市" name="ss" v-if="4 == formData.current || formData.add">
			<dragTextarea :list="formData.ssList" type="ss"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.ss" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="晋江市" name="jj" v-if="5 == formData.current || formData.add">
			<dragTextarea :list="formData.jjList" type="jj"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.jj" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="南安市" name="na" v-if="6 == formData.current || formData.add">
			<dragTextarea :list="formData.naList" type="na"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.na" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="惠安县" name="ha" v-if="7 == formData.current || formData.add">
			<dragTextarea :list="formData.haList" type="ha"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.ha" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="安溪县" name="ax" v-if="8 == formData.current || formData.add">
			<dragTextarea :list="formData.axList" type="ax"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.ax" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="永春县" name="yc" v-if="9 == formData.current || formData.add">
			<dragTextarea :list="formData.ycList" type="yc"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.yc" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="德化县" name="dh" v-if="10 == formData.current || formData.add">
			<dragTextarea :list="formData.dhList" type="dh"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.dh" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="泉州开发区" name="kf" v-if="11 == formData.current || formData.add">
			<dragTextarea :list="formData.kfList" type="kf"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.kf" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
		  <uni-forms-item label="泉州台商投资区" name="ts" v-if="12 == formData.current || formData.add">
		    <dragTextarea :list="formData.tsList" type="ts"></dragTextarea>
			<!-- <uni-easyinput type="textarea" autoHeight v-model="formData.ts" placeholder="请输入内容"></uni-easyinput> -->
		  </uni-forms-item>
     </uni-forms>
      <button class="button-blue" style="position: fixed;bottom: 0;" @click="save()">保存</button>
    </view>
  </view>
</template>

<script>
  import { checkPermi } from "@/utils/permission";
  import { basicsumApi } from '@/api/module';
  import dragTextarea from '@/components/DragTextarea/index';
  import elementarea from '@/components/DragTextarea/element';
  import { Debounce } from '@/utils/common';

  export default {
    components:{
	  dragTextarea,
	  elementarea
    },
    data() {
      return {
	   //表单
	   formData: {
	   },
		//表单规则判断
        rules: {
		  date: {
		    rules: [{
			  required: true,
			  errorMessage: '请选择日期'
		    }]
		  }
        },	 
      }
    },
    async onLoad(option) {
		if(option.formData){
			let covertData = JSON.parse(option.formData);
			this.formData = {...covertData};
			this.handleList(this.formData);
		}
		
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
	  dateChange(val){
			if(val){
				//查询数据
				uni.showLoading();
				let search = new Object();
				let flag = this.formData.add;
				let current = this.formData.current;
				let lc = this.formData.lc;
				let fz = this.formData.fz;
				let lj = this.formData.lj;
				let qg = this.formData.qg;
				let ss = this.formData.ss;
				let jj = this.formData.jj;
				let na = this.formData.na;
				let ha = this.formData.ha;
				let ax = this.formData.ax;
				let yc = this.formData.yc;
				let dh = this.formData.dh;
				let kf = this.formData.kf;
				let ts = this.formData.ts;
				let id = this.formData.id;
				
				let lcList = this.formData.lcList;
				let fzList = this.formData.fzList;
				let ljList = this.formData.ljList;
				let qgList = this.formData.qgList;
				let ssList = this.formData.ssList;
				let jjList = this.formData.jjList;
				let naList = this.formData.naList;
				let haList = this.formData.haList;
				let axList = this.formData.axList;
				let ycList = this.formData.ycList;
				let dhList = this.formData.dhList;
				let kfList = this.formData.kfList;
				let tsList = this.formData.tsList;
				search.date = val;
				basicsumApi.listBasicsum(search).then(response =>{
					if(response.rows.length > 0){
						this.formData = response.rows[0];
						this.formData.add = flag;
						this.handleList(this.formData);
					}else{
						this.formData = {};
						this.formData.add = flag;
						this.formData.date = search.date;
						if(!id){
						  this.formData.lc = lc;
						  this.formData.fz = fz;
						  this.formData.lj = lj;
						  this.formData.qg = qg;
						  this.formData.ss = ss;
						  this.formData.jj = jj;
						  this.formData.na = na;
						  this.formData.ha = ha;
						  this.formData.ax = ax;
						  this.formData.yc = yc;
						  this.formData.dh = dh;
						  this.formData.kf = kf;
						  this.formData.ts = ts;
						  
						  this.formData.lcList = lcList;
						  this.formData.fzList = fzList;
						  this.formData.ljList = ljList;
						  this.formData.qgList = qgList;
						  this.formData.ssList = ssList;
						  this.formData.jjList = jjList;
						  this.formData.naList = naList;
						  this.formData.haList = haList;
						  this.formData.axList = axList;
						  this.formData.ycList = ycList;
						  this.formData.dhList = dhList;
						  this.formData.kfList = kfList;
						  this.formData.tsList = tsList;
						}
						this.formData.weeks = this.getWeeks(val);
						this.formData.children = [];
					}
					this.formData.current = current;
					uni.hideLoading();
				});
			}else{
				this.formData.weeks = null;
			}
		},
	  //权限
	  checkPermi,
	  save:Debounce(function(){
		  this.copyData();
		  this.$refs.form.validate().then(res=>{
			  let that = this;
			  uni.showLoading();
			  this.formData.createTime = null;
			  this.formData.updateTime = null;
			 // this.formData.weeks = this.getWeeks(this.formData.date);
			  basicsumApi.saveAllBasicsum(this.formData).then(data => {
				  uni.hideLoading();
				  this.$modal.msgSuccess('操作成功');
				  setTimeout(function(){
					  that.$tab.navigateTo('/pages/module/basicsum/list');
				  },350)
				  
			  })
		  })
	  }),
	  
	  //整理数据
	  copyData(){
	    let userId = this.$store.getters.userId;
	    let nickName = this.$store.getters.nickName;
	    let flag = this.formData.children.some(user => user.userId.toString() === userId);
	    let ownData;
	    if(flag){
	        ownData = this.formData.children.filter(user => user.userId.toString() === userId)[0];
	    }else{
	        ownData = new Object();
	        ownData.userId = userId;
	        ownData.basicsumId = this.formData.id;
	        ownData.nickName = nickName;
	    }
		ownData.createTime = null;
		ownData.updateTime = null;
		this.sumData(ownData);
	    this.formData.basicsumCtrl = ownData;
	    this.sumFormData();
	  },
	  
	  //整理当前用户数据
	  sumData(item){
	    let userId = this.$store.getters.userId;
	    let lcData = this.formData.lcList.find(item => item.userId.toString() === userId);//find()查找第一个数据
	    let fzData = this.formData.fzList.find(item => item.userId.toString() === userId);
	    let qgData = this.formData.qgList.find(item => item.userId.toString() === userId);
	    let ljData = this.formData.ljList.find(item => item.userId.toString() === userId);
	    let ssData = this.formData.ssList.find(item => item.userId.toString() === userId);
	    let jjData = this.formData.jjList.find(item => item.userId.toString() === userId);
	    let naData = this.formData.naList.find(item => item.userId.toString() === userId);
	    let haData = this.formData.haList.find(item => item.userId.toString() === userId);
	    let axData = this.formData.axList.find(item => item.userId.toString() === userId);
	    let ycData = this.formData.ycList.find(item => item.userId.toString() === userId);
	    let dhData = this.formData.dhList.find(item => item.userId.toString() === userId);
	    let kfData = this.formData.kfList.find(item => item.userId.toString() === userId);
	    let tsData = this.formData.tsList.find(item => item.userId.toString() === userId);
	    item.lc = lcData.lc ? lcData.lc : null;
	    item.fz = fzData.fz ? fzData.fz : null;
	    item.qg = qgData.qg ? qgData.qg : null;
	    item.lj = ljData.lj ? ljData.lj : null;
	    item.ss = ssData.ss ? ssData.ss : null;
	    item.jj = jjData.jj ? jjData.jj : null;
	    item.na = naData.na ? naData.na : null;
	    item.ha = haData.ha ? haData.ha : null;
	    item.ax = axData.ax ? axData.ax : null;
	    item.yc = ycData.yc ? ycData.yc : null;
	    item.dh = dhData.dh ? dhData.dh : null;
	    item.kf = kfData.kf ? kfData.kf : null;
	    item.ts = tsData.ts ? tsData.ts : null;
	  },
	  
	  //整理basicsum主表数据
	  sumFormData(){
	    this.formData.lc = this.formData.lcList.reduce((accumulator, item) => {
	      return item.lc ? accumulator + item.lc + "\n" : null;
	    }, '');
	    this.formData.fz = this.formData.fzList.reduce((accumulator, item) => {
	      return item.fz ? accumulator + item.fz + "\n" : null;
	    }, '');
	    this.formData.qg = this.formData.qgList.reduce((accumulator, item) => {
	      return item.qg ? accumulator + item.qg + "\n" : null;
	    }, '');
	    this.formData.lj = this.formData.ljList.reduce((accumulator, item) => {
	      return item.lj ? accumulator + item.lj + "\n" : null;
	    }, '');
	    this.formData.ss = this.formData.ssList.reduce((accumulator, item) => {
	      return item.ss ? accumulator + item.ss + "\n" : null;
	    }, '');
	    this.formData.jj = this.formData.jjList.reduce((accumulator, item) => {
	      return item.jj ? accumulator + item.jj + "\n" : null;
	    }, '');
	    this.formData.na = this.formData.naList.reduce((accumulator, item) => {
	      return item.na ? accumulator + item.na + "\n" : null;
	    }, '');
	    this.formData.ha = this.formData.haList.reduce((accumulator, item) => {
	      return item.ha ? accumulator + item.ha + "\n" : null;
	    }, '');
	    this.formData.ax = this.formData.axList.reduce((accumulator, item) => {
	      return item.ax ? accumulator + item.ax + "\n" : null;
	    }, '');
	    this.formData.yc = this.formData.ycList.reduce((accumulator, item) => {
	      return item.yc ? accumulator + item.yc + "\n" : null;
	    }, '');
	    this.formData.dh = this.formData.dhList.reduce((accumulator, item) => {
	      return item.dh ? accumulator + item.dh + "\n" : null;
	    }, '');
	    this.formData.kf = this.formData.kfList.reduce((accumulator, item) => {
	      return item.kf ? accumulator + item.kf + "\n" : null;
	    }, '');
	    this.formData.ts = this.formData.tsList.reduce((accumulator, item) => {
	      return item.ts ? accumulator + item.ts + "\n" : null;
	    }, '');
	  },
	  
	  
	  getWeeks(val){
	  	var year = val.substr(0,4);
	  	var month = val.substr(5,2);
	  	var day = val.substr(8,2);
	  	let weeks = this.getYearWeeks(year,month,day);
	  	return weeks;
	  },
	  
	  handleList(row){
	     let userId = this.$store.getters.userId;
	     let basicsumId = row.id;
		 const current = row.current;
	     let lc = row.lc;
	     let fz = row.fz;
	     let lj = row.lj;
	     let qg = row.qg;
	     let ss = row.ss;
	     let jj = row.jj;
	     let na = row.na;
	     let ha = row.ha;
	     let ax = row.ax;
	     let yc = row.yc;
	     let dh = row.dh;
	     let kf = row.kf;
	     let ts = row.ts;
	     let lcList = [];
	     let fzList = [];
	     let ljList = [];
	     let qgList = [];
	     let ssList = [];
	     let jjList = [];
	     let naList = [];
	     let haList = [];
	     let axList = [];
	     let ycList = [];
	     let dhList = [];
	     let kfList = [];
	     let tsList = [];
		
	     for(let basicsumCtrl of row.children){
		   if(basicsumCtrl.lc && lc){
			 basicsumCtrl.sort = lc.indexOf(basicsumCtrl.lc);
			 lcList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.fz && fz){
			 basicsumCtrl.sort = fz.indexOf(basicsumCtrl.fz);
			 fzList.push(basicsumCtrl);
		   }

		   if(basicsumCtrl.lj && lj){
			 basicsumCtrl.sort = lj.indexOf(basicsumCtrl.lj);
			 ljList.push(basicsumCtrl);
		   }
			if(basicsumCtrl.qg && qg){
			  basicsumCtrl.sort = qg.indexOf(basicsumCtrl.qg);
			  qgList.push(basicsumCtrl);
			}
		   if(basicsumCtrl.ss && ss){
			 basicsumCtrl.sort = ss.indexOf(basicsumCtrl.ss);
			 ssList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.jj && jj){
			 basicsumCtrl.sort = jj.indexOf(basicsumCtrl.jj);
			 jjList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.na && na){
			 basicsumCtrl.sort = na.indexOf(basicsumCtrl.na);
			 naList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.ha && ha){
			 basicsumCtrl.sort = ha.indexOf(basicsumCtrl.ha);
			 haList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.ax && ax){
			 basicsumCtrl.sort = ax.indexOf(basicsumCtrl.ax);
			 axList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.yc && yc){
			 basicsumCtrl.sort = yc.indexOf(basicsumCtrl.yc);
			 ycList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.dh && dh){
			 basicsumCtrl.sort = dh.indexOf(basicsumCtrl.dh);
			 dhList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.kf && kf){
			 basicsumCtrl.sort = kf.indexOf(basicsumCtrl.kf);
			 kfList.push(basicsumCtrl);
		   }
		   if(basicsumCtrl.ts && ts){
			 basicsumCtrl.sort = ts.indexOf(basicsumCtrl.ts);
			 tsList.push(basicsumCtrl);
		   }
		   
	     }
	     lcList.sort((a, b) => a.sort - b.sort);
	     fzList.sort((a, b) => a.sort - b.sort);
	     ljList.sort((a, b) => a.sort - b.sort);
	     qgList.sort((a, b) => a.sort - b.sort);
	     ssList.sort((a, b) => a.sort - b.sort);
	     jjList.sort((a, b) => a.sort - b.sort);
	     naList.sort((a, b) => a.sort - b.sort);
	     haList.sort((a, b) => a.sort - b.sort);
	     axList.sort((a, b) => a.sort - b.sort);
	     ycList.sort((a, b) => a.sort - b.sort);
	     dhList.sort((a, b) => a.sort - b.sort);
	     kfList.sort((a, b) => a.sort - b.sort);
	     tsList.sort((a, b) => a.sort - b.sort);
	     let lcFlag = lcList.some(user => user.userId.toString() === userId);
	     if(!lcFlag){
	       let lcVal = {};
	       lcVal.userId = userId;
	       lcVal.lc = "";
	       lcList.push(lcVal);
	     }
	  
	    let fzFlag = fzList.some(user => user.userId.toString() === userId);
	     if(!fzFlag){
	       let fzVal = {};
	       fzVal.userId = userId;
	       fzVal.fz = "";
	       fzList.push(fzVal);
	     }
	  
	     let ljFlag = ljList.some(user => user.userId.toString() === userId);
	     if(!ljFlag){
	       let ljVal = {};
	       ljVal.userId = userId;
	       ljVal.lj = "";
	       ljList.push(ljVal);
	     }
	  
	     let qgFlag = qgList.some(user => user.userId.toString() === userId);
	     if(!qgFlag){
	       let qgVal = {};
	       qgVal.userId = userId;
	       qgVal.qg = "";
	       qgList.push(qgVal);
	     }
	  
	     let ssFlag = ssList.some(user => user.userId.toString() === userId);
	     if(!ssFlag){
	       let ssVal = {};
	       ssVal.userId = userId;
	       ssVal.ss = "";
	       ssList.push(ssVal);
	     }
	  
	     let jjFlag = jjList.some(user => user.userId.toString() === userId);
	     if(!jjFlag){
	       let jjVal = {};
	       jjVal.userId = userId;
	       jjVal.jj = "";
	       jjList.push(jjVal);
	     }
	  
	     let naFlag = naList.some(user => user.userId.toString() === userId);
	     if(!naFlag){
	       let naVal = {};
	       naVal.userId = userId;
	       naVal.na = "";
	       naList.push(naVal);
	     }
	  
	     let haFlag = haList.some(user => user.userId.toString() === userId);
	     if(!haFlag){
	       let haVal = {};
	       haVal.userId = userId;
	       haVal.ha = "";
	       haList.push(haVal);
	     }
	  
	     let axFlag = axList.some(user => user.userId.toString() === userId);
	     if(!axFlag){
	       let axVal = {};
	       axVal.userId = userId;
	       axVal.ax = "";
	       axList.push(axVal);
	     }
	  
	     let ycFlag = ycList.some(user => user.userId.toString() === userId);
	     if(!ycFlag){
	       let ycVal = {};
	       ycVal.userId = userId;
	       ycVal.yc = "";
	       ycList.push(ycVal);
	     }
	  
	     let dhFlag = dhList.some(user => user.userId.toString() === userId);
	     if(!dhFlag){
	       let dhVal = {};
	       dhVal.userId = userId;
	       dhVal.dh = "";
	       dhList.push(dhVal);
	     }
	  
	     let kfFlag = kfList.some(user => user.userId.toString() === userId);
	     if(!kfFlag){
	       let kfVal = {};
	       kfVal.userId = userId;
	       kfVal.kf = "";
	       kfList.push(kfVal);
	     }
	  
	     let tsFlag = tsList.some(user => user.userId.toString() === userId);
	     if(!tsFlag){
	       let tsVal = {};
	       tsVal.userId = userId;
	       tsVal.ts = "";
	       tsList.push(tsVal);
	     }
	     row.lcList = lcList;
	     row.fzList = fzList;
	     row.ljList = ljList;
	     row.qgList = qgList;
	     row.ssList = ssList;
	     row.jjList = jjList;
	     row.naList = naList;
	     row.haList = haList;
	     row.ycList = ycList;
	     row.axList = axList;
	     row.dhList = dhList;
	     row.kfList = kfList;
	     row.tsList = tsList;
	  },
	  
    }
  }
</script>

<style lang="scss">
	.drag {
	  position: absolute;
	  left: 0;
	  top: 0;
	  width: 100px;
	  height: 100px;
	  background-color: #f00;
	  color: #fff;
	  text-align: center;
	  line-height: 100px;
	}
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button-blue {
    display: flex;
    align-items: center;
    line-height: 35px;
    margin-left: 10px;
  }
</style>
